The mDialogBox Component: Not Your Father's Dialog Box
Table of Contents
Putting mDialogBox to Work
The following two examples are complete and practical. The first (app_1.fla) is a simple confirmation dialog box; the second (app_2.fla) gives the user access to several preferences that get saved in a local shared object. I discuss in detail what's going on after the following steps.
Note: You can download the source files for these examples on the first page of this article, but you'll have to install the application into Central using a web server (as part of the security model of Central). Note that the productID used in the provided product.xml file is the one and only testing productID used in many other examples. Because you can't install two applications with the same productID, either be sure first to uninstall any sample applications (such as from other tutorials I've written) or replace productID with a unique number provided to you from the Macromedia Central Product ID Registration generator.
Simple Confirmation Dialog Box
This example creates the simple modal confirmation dialog box shown in Figure 4.
Figure 4. This simple modal confirmation dialog box results from the steps you follow.
- Select File > Publish Settings, click the Flash tab, and set the Version drop-down menu to Flash Player 6.
- To make the insides of your dialog box, drag two mPushButton components onto the Stage. Set one label to yes and its clickHandler to doYes and set the other label to no and its clickHandler to doNo. In addition, you can include a graphic question mark like the one I grabbed from the Application Graphics Library that ships with Macromedia DevNet Resource Kit Volume 4.
- Use the Text tool to draw a text field above the two buttons and make the margins wide. Set the text field to Dynamic and give it an instance name prompt_txt. Deselect the editable option and set Line Type to Multiline.
- Select everything and convert to symbol (press F8). Select the movie clip and be sure to click the top-left justification (shown later in Figure 6). Name it confirmation and click OK.
- Remove the instance of this confirmation symbol from the Stage (it's safe inside your Library). Select the item in the Library and choose Linkage from the Library's options menu or when you right-click it (Option-click on Mac). Click the Export for ActionScript option and leave the linkage identifier in its default "confirmation." Click OK.
- Drag the following components onto the Stage: two mPushButton, one mTextInputField, one mListBox, and one mDialogBox.
- Give one button the label add and the clickHandler doAddToList. Give the other button the label clear and the clickHandler doClear.
- Give mListBox the instance name myList and mTextInputField the instance name myText. (While you're on mTextInputField, you can set its Show Label parameter to false.)
- Click the first keyframe and paste the following code in it:
01 var dp = new Central.DataProviderClass();
02 dp.addItem({label:"n/a", data: "confirmation"});
03 myDialog.setDataProvider(dp);
04
05 myDialog.setIsModal(true);
06 myDialog.showClose(false);
07 myDialog.setTitle("confirmation");
08 myDialog.doClose();
09
10 doClear=function(){
11 myDialog.doOpen();
12 var dialogContent=myDialog.getSelectedContent();
13 dialogContent.prompt_txt.text="Are you sure?";
14
15 dialogContent.doNo=function(){
16 myDialog.doClose();
17 }
18
19 dialogContent.doYes=function(){
20 myList.removeAll();
21 myDialog.doClose();
22 }
23 }
24
25 doAddToList=function(){
26 myList.addItem(myText.getValue());
27 myText.setValue("");
28 }
29
30 Central.initApplication(this,this);
In the first three lines I create a DataProvider, populate it (with
just one item), and then associate the data with the myDialog instance.
Lines 5–8 do the following initialization: set it to modal, remove
the close box, set the title, and finally close it, so it's closed
initially. It's a good habit to close it after you populate it. In
the case of setting it modal, for example, it reappears even if you
already closed it, so it's a good habit to close it as the last step
after populating it.
If you jump to line 25 you can see the Add button's change handler defined. Line 30 has the obligatory initApplication() call.
As shown in lines 10–23, when the user clicks the Clear button it triggers the doClear() function. Line 11 opens the dialog box and line 12 grabs a reference to a clip embedded inside the dialog box (using the built-in getSelectedConent() method). That reference (the homemade variable dialogContent) is used to populate a text field (line 13) and then define two callbacks (doNo() in line 15 and doYes() in line 19). Those are the click handlers for two buttons inside the confirmation clip. Both call the doClose() method but doYes() additionally calls the ListBox's removeAll() built-in method (line 20).
It's much easier to reach down into mDialogBox's content clips, using getSelectedContent(), than it is for the embedded clip to reference the dialog box itself. If for no other reason, the approach of talking down to the clip is better because nearly all the code is high up in the main timeline. (I discuss this topic more in the Tips section.)
Multiscreen Preferences Dialog Box
This example creates the multiscreen preferences dialog box shown in Figure 5 that contains three categories: gender, birthday, and software preferences.
Figure 5. This multiscreen preferences dialog box results from the steps you follow.
-
For the gender clip, drag one mPushButton component and two mRadioButton components onto the Stage. In addition, create some static text that reads "Pick your gender." Use the Property inspector to set the PushButton label to Next and Change Handler to doNext. (By the way, each of these three clips needs a copy of this button so once you've set the parameters make a duplicate on the Stage that you can copy and paste.) The mRadioButton components need the following parameters changed:
Label: Male (for Button 1), Female (for Button 2)
Group Name: genderGroup (for both buttons)
Data: 1 (for Button 1), 2 (for Button 2)
Change Handler: pickGender (for both buttons) - Select the text and buttons and press F8 to convert to a movie clip. Be sure to select the top-left registration option and name the symbol gender. You can remove it from the screen because later you'll set the linkage for all three clips.
- For the birthday preference, drag an mCalendar component onto the Stage. Give it the instance name birthday and change the selection type parameter to single. Set Change Handler to pickBirthday. Create a static text block that reads "Set your birthday." Finally, create a Next button the same as you did in Step 1 (label Next and Change Handler doNext). Select the calendar, text, and Next button and convert to symbol (press F8). Give this symbol the name birthday, click OK, and then delete the instance remaining on the Stage.
- The software preference uses three mCheckBox components (plus a Next button and static text for a title). Drag three instances of the mCheckBox component onto the Stage and give them the following instance names: software_1, software_2, and software_3. Set each one's Change Handler to pickSoftware. Finally, create a static text field that reads "Pick your software preferences." Make a Next button as you did in Steps 1 and 3. Finally, select the buttons and text and convert to symbol (press F8). Name the symbol software, click OK, and then delete the instance on the Stage.
- For each of the three symbols created (gender, birthday, and software), set a linkage identifier that matches their name. All you have to do is open the Library and select one at a time, pick Linkage from the Library's options menu, click the first check box for Export for ActionScript, and then click OK. Finally, if you're not using the same old file from the last exercise, drag an instance of the mDialogBox component and give it an instance name myDialog. Set the Modal Dialog parameter to true.
-
Click the first keyframe and paste the following code in it:
01 var dp = new Central.DataProviderClass(); 02 dp.addItem({label:"Gender", data: "gender"}); 03 dp.addItem({label:"Birthday", data: "birthday"}); 04 dp.addItem({label:"Software", data: "software"}); 05 myDialog.setDataProvider(dp); 06 07 myDialog.setTitle("Preferences"); 08 myDialog.doClose(); 09 showPreferences=function(){ 10 myDialog.doOpen(); 11 } 12 onActivate=function(){ 13 prefs_so=SharedObject.getLocal("prefs"); 14 } 15 16 var obj = new Object(); 17 obj.onContentChanged = function(ref){ 18 //get a reference to the dialog content 19 var dialogContent=myDialog.getSelectedContent(); 20 21 //=============populate: 22 //for gender: 23 var theirGender=prefs_so.data.gender 24 if(theirGender!=undefined){ 25 dialogContent.genderGroup.setValue(theirGender); 26 } 27 28 //for birthday preference: 29 var theirBirthday=prefs_so.data.birthday 30 if(theirBirthday!=undefined){ 31 dialogContent.birthday.setSelectedItem(theirBirthday); 32 dialogContent.birthday.setDisplayedMonth(theirBirthday); 33 } 34 35 //for software preference: 36 var theirSoftware=prefs_so.data.software; 37 if(theirSoftware==undefined){ 38 prefs_so.data.software=[0,0,0]; 39 theirSoftware=prefs_so.data.software; 40 } 41 dialogContent.software_1.setValue(theirSoftware[0]); 42 dialogContent.software_2.setValue(theirSoftware[1]); 43 dialogContent.software_3.setValue(theirSoftware[2]); 44 //give each button a "value" 45 dialogContent.software_1.value=0; 46 dialogContent.software_2.value=1; 47 dialogContent.software_3.value=2; 48 49 //=============set up callbacks 50 //for all: 51 dialogContent.doNext=function(){ 52 myDialog.setSelectedNextIndex(); 53 } 54 //for gender: 55 dialogContent.pickGender=function(ref){ 56 prefs_so.data.gender=ref.getData(); 57 } 58 //for birthday preference: 59 dialogContent.pickBirthday=function(ref){ 60 prefs_so.data.birthday=ref.getSelectedItems()[0]; 61 } 62 //for birthday preference: 63 dialogContent.pickSoftware=function(ref){ 64 prefs_so.data.software[ref.value]=ref.getValue(); 65 } 66 } 67 myDialog.addListener(obj) 68 69 Central.initApplication(this,this);
This time creating the DataProvider and associating it with the dialog box uses lines 1–5, because there are three screens. Lines 7 and 8 simply set the title and close the dialog box initially. Lines 9–11 set up the callback that Central calls if the user chooses to view preferences. When this happens we just open the dialog box (line 10). Lines 12–14 trap the onActivate method that Central triggers once the application initializes. Here we just grab the shared object in the "prefs" file. Way at the bottom (line 69) we initialize the Central application.
The bulk of code is stored inside the onContentChanged event listener on
the obj object, which is first defined on line 16 and later
added as a listener to the dialog box on line 67. That is, every time the
content changes (including whenever the dialog box simply opens) we run
all the code from lines 18–65. This may seem like overkill—after all, if
users are only opening the dialog box to see their gender preference there
is no need to set up the birthday options—but unless you want to check which
section of the dialog box it has changed to, then this code is fine even
if some of it is superfluous.
There are two main tasks I perform every time the dialog box changes: populate
the clip to reflect its previous settings and define the click/change handlers
for the various buttons contained. First consider the population portion
(lines 21–47). In order to do this I start on line 19 by grabbing a reference
to the selected clip. For the gender I first see what value is stored in
the gender property of the shared object (line 23). Naturally,
this is undefined initially but, if not, I set the value for the RadioButton
group called genderGroup accordingly (line 25). Similarly, the birthday
preference is accessed in line 29 and, provided it's not undefined,
I trigger two mCalendar component methods: setSelectedItem() to select the
date and setDisplayedMonth() so you can see the date. Populating the radio
buttons is a tad more involved. I designed the software property
to contain an array. Actually, you can see in lines 38–39 that if that property
is indeed undefined, I initialize it with an array full of zeros. Lines
41–43 use the built-in setValue() method to make the buttons' check marks
appear. Lines 45–47 create and set a property I made up called value.
That is, check boxes don't really have a value property but,
as you'll see later (on line 64), this makes it easy to determine which
button was clicked.
For the click handlers (lines 49–65) you can see some consistencies. The
doNext() method (lines 51–53) simply triggers the built-in setSelectedNextIndex()
which moves the mDialogBox component to the next page of a multiscreen display,
as if the user used the drop-down menu to go to the next screen. Remember
that the buttons inside the three clips trigger one of the three change
handlers: pickGender(), pickBirthday(), and pickSoftware(). When a Central
component triggers a change handler, it passes a reference of itself as
a parameter. So in line 56 I just use the built-in RadioButton method getData()
when setting the gender property. Line 60 uses the Calendar's
getSelectedItems() method when setting the birthday property
(but do notice that we only need the first item—in the zero slot—of the
array returned). Line 64 is slightly more tricky because we need to know
which slot in the array for the software property to set—that's
where the homemade value property (that I created in lines
45–47) comes into play.
I like this example because even though you might not need to store the user's gender and birthday, you can apply and expand this structure to any set of preferences. This exercise lets you play with a couple of the other components too!